home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsflip.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.3 KB  |  403 lines

  1. /* Copyright (C) 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsflip.c,v 1.2 2000/09/19 19:00:28 lpd Exp $ */
  20. /* Routines for "flipping" image data */
  21. #include "gx.h"
  22. #include "gserrors.h"        /* for rangecheck in sample macros */
  23. #include "gsbitops.h"
  24. #include "gsbittab.h"
  25. #include "gsflip.h"
  26.  
  27. #define ARCH_HAS_BYTE_REGS 1
  28.  
  29. /* Transpose a block of bits between registers. */
  30. #define TRANSPOSE(r,s,mask,shift)\
  31.   r ^= (temp = ((s >> shift) ^ r) & mask);\
  32.   s ^= temp << shift
  33.  
  34. /* Define the size of byte temporaries.  On Intel CPUs, this should be */
  35. /* byte, but on all other CPUs, it should be uint. */
  36. #if ARCH_HAS_BYTE_REGS
  37. typedef byte byte_var;
  38. #else
  39. typedef uint byte_var;
  40. #endif
  41.  
  42. #define VTAB(v80,v40,v20,v10,v8,v4,v2,v1)\
  43.   bit_table_8(0,v80,v40,v20,v10,v8,v4,v2,v1)
  44.  
  45. /* Convert 3Mx1 to 3x1. */
  46. private int
  47. flip3x1(byte * buffer, const byte ** planes, int offset, int nbytes)
  48. {
  49.     byte *out = buffer;
  50.     const byte *in1 = planes[0] + offset;
  51.     const byte *in2 = planes[1] + offset;
  52.     const byte *in3 = planes[2] + offset;
  53.     int n = nbytes;
  54.     static const bits32 tab3x1[256] = {
  55.     VTAB(0x800000, 0x100000, 0x20000, 0x4000, 0x800, 0x100, 0x20, 4)
  56.     };
  57.  
  58.     for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
  59.     bits32 b24 = tab3x1[*in1] | (tab3x1[*in2] >> 1) | (tab3x1[*in3] >> 2);
  60.  
  61.     out[0] = (byte) (b24 >> 16);
  62.     out[1] = (byte) (b24 >> 8);
  63.     out[2] = (byte) b24;
  64.     }
  65.     return 0;
  66. }
  67.  
  68. /* Convert 3Mx2 to 3x2. */
  69. private int
  70. flip3x2(byte * buffer, const byte ** planes, int offset, int nbytes)
  71. {
  72.     byte *out = buffer;
  73.     const byte *in1 = planes[0] + offset;
  74.     const byte *in2 = planes[1] + offset;
  75.     const byte *in3 = planes[2] + offset;
  76.     int n = nbytes;
  77.     static const bits32 tab3x2[256] = {
  78.     VTAB(0x800000, 0x400000, 0x20000, 0x10000, 0x800, 0x400, 0x20, 0x10)
  79.     };
  80.  
  81.     for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
  82.     bits32 b24 = tab3x2[*in1] | (tab3x2[*in2] >> 2) | (tab3x2[*in3] >> 4);
  83.  
  84.     out[0] = (byte) (b24 >> 16);
  85.     out[1] = (byte) (b24 >> 8);
  86.     out[2] = (byte) b24;
  87.     }
  88.     return 0;
  89. }
  90.  
  91. /* Convert 3Mx4 to 3x4. */
  92. private int
  93. flip3x4(byte * buffer, const byte ** planes, int offset, int nbytes)
  94. {
  95.     byte *out = buffer;
  96.     const byte *in1 = planes[0] + offset;
  97.     const byte *in2 = planes[1] + offset;
  98.     const byte *in3 = planes[2] + offset;
  99.     int n = nbytes;
  100.  
  101.     for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
  102.     byte_var b1 = *in1, b2 = *in2, b3 = *in3;
  103.  
  104.     out[0] = (b1 & 0xf0) | (b2 >> 4);
  105.     out[1] = (b3 & 0xf0) | (b1 & 0xf);
  106.     out[2] = (byte) (b2 << 4) | (b3 & 0xf);
  107.     }
  108.     return 0;
  109. }
  110.  
  111. /* Convert 3Mx8 to 3x8. */
  112. private int
  113. flip3x8(byte * buffer, const byte ** planes, int offset, int nbytes)
  114. {
  115.     byte *out = buffer;
  116.     const byte *in1 = planes[0] + offset;
  117.     const byte *in2 = planes[1] + offset;
  118.     const byte *in3 = planes[2] + offset;
  119.     int n = nbytes;
  120.  
  121.     for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
  122.     out[0] = *in1;
  123.     out[1] = *in2;
  124.     out[2] = *in3;
  125.     }
  126.     return 0;
  127. }
  128.  
  129. /* Convert 3Mx12 to 3x12. */
  130. private int
  131. flip3x12(byte * buffer, const byte ** planes, int offset, int nbytes)
  132. {
  133.     byte *out = buffer;
  134.     const byte *pa = planes[0] + offset;
  135.     const byte *pb = planes[1] + offset;
  136.     const byte *pc = planes[2] + offset;
  137.     int n = nbytes;
  138.  
  139.     /*
  140.      * We assume that the input is an integral number of pixels, and
  141.      * round up n to a multiple of 3.
  142.      */
  143.     for (; n > 0; out += 9, pa += 3, pb += 3, pc += 3, n -= 3) {
  144.     byte_var a1 = pa[1], b0 = pb[0], b1 = pb[1], b2 = pb[2], c1 = pc[1];
  145.  
  146.     out[0] = pa[0];
  147.     out[1] = (a1 & 0xf0) | (b0 >> 4);
  148.     out[2] = (byte) ((b0 << 4) | (b1 >> 4));
  149.     out[3] = pc[0];
  150.     out[4] = (c1 & 0xf0) | (a1 & 0xf);
  151.     out[5] = pa[2];
  152.     out[6] = (byte) ((b1 << 4) | (b2 >> 4));
  153.     out[7] = (byte) ((b2 << 4) | (c1 & 0xf));
  154.     out[8] = pc[2];
  155.     }
  156.     return 0;
  157. }
  158.  
  159. /* Convert 4Mx1 to 4x1. */
  160. private int
  161. flip4x1(byte * buffer, const byte ** planes, int offset, int nbytes)
  162. {
  163.     byte *out = buffer;
  164.     const byte *in1 = planes[0] + offset;
  165.     const byte *in2 = planes[1] + offset;
  166.     const byte *in3 = planes[2] + offset;
  167.     const byte *in4 = planes[3] + offset;
  168.     int n = nbytes;
  169.  
  170.     for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
  171.     byte_var b1 = *in1, b2 = *in2, b3 = *in3, b4 = *in4;
  172.     byte_var temp;
  173.  
  174.     /* Transpose blocks of 1 */
  175.     TRANSPOSE(b1, b2, 0x55, 1);
  176.     TRANSPOSE(b3, b4, 0x55, 1);
  177.     /* Transpose blocks of 2 */
  178.     TRANSPOSE(b1, b3, 0x33, 2);
  179.     TRANSPOSE(b2, b4, 0x33, 2);
  180.     /* There's probably a faster way to do this.... */
  181.     out[0] = (b1 & 0xf0) | (b2 >> 4);
  182.     out[1] = (b3 & 0xf0) | (b4 >> 4);
  183.     out[2] = (byte) ((b1 << 4) | (b2 & 0xf));
  184.     out[3] = (byte) ((b3 << 4) | (b4 & 0xf));
  185.     }
  186.     return 0;
  187. }
  188.  
  189. /* Convert 4Mx2 to 4x2. */
  190. private int
  191. flip4x2(byte * buffer, const byte ** planes, int offset, int nbytes)
  192. {
  193.     byte *out = buffer;
  194.     const byte *in1 = planes[0] + offset;
  195.     const byte *in2 = planes[1] + offset;
  196.     const byte *in3 = planes[2] + offset;
  197.     const byte *in4 = planes[3] + offset;
  198.     int n = nbytes;
  199.  
  200.     for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
  201.     byte_var b1 = *in1, b2 = *in2, b3 = *in3, b4 = *in4;
  202.     byte_var temp;
  203.  
  204.     /* Transpose blocks of 4x2 */
  205.     TRANSPOSE(b1, b3, 0x0f, 4);
  206.     TRANSPOSE(b2, b4, 0x0f, 4);
  207.     /* Transpose blocks of 2x1 */
  208.     TRANSPOSE(b1, b2, 0x33, 2);
  209.     TRANSPOSE(b3, b4, 0x33, 2);
  210.     out[0] = b1;
  211.     out[1] = b2;
  212.     out[2] = b3;
  213.     out[3] = b4;
  214.     }
  215.     return 0;
  216. }
  217.  
  218. /* Convert 4Mx4 to 4x4. */
  219. private int
  220. flip4x4(byte * buffer, const byte ** planes, int offset, int nbytes)
  221. {
  222.     byte *out = buffer;
  223.     const byte *in1 = planes[0] + offset;
  224.     const byte *in2 = planes[1] + offset;
  225.     const byte *in3 = planes[2] + offset;
  226.     const byte *in4 = planes[3] + offset;
  227.     int n = nbytes;
  228.  
  229.     for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
  230.     byte_var b1 = *in1, b2 = *in2, b3 = *in3, b4 = *in4;
  231.  
  232.     out[0] = (b1 & 0xf0) | (b2 >> 4);
  233.     out[1] = (b3 & 0xf0) | (b4 >> 4);
  234.     out[2] = (byte) ((b1 << 4) | (b2 & 0xf));
  235.     out[3] = (byte) ((b3 << 4) | (b4 & 0xf));
  236.     }
  237.     return 0;
  238. }
  239.  
  240. /* Convert 4Mx8 to 4x8. */
  241. private int
  242. flip4x8(byte * buffer, const byte ** planes, int offset, int nbytes)
  243. {
  244.     byte *out = buffer;
  245.     const byte *in1 = planes[0] + offset;
  246.     const byte *in2 = planes[1] + offset;
  247.     const byte *in3 = planes[2] + offset;
  248.     const byte *in4 = planes[3] + offset;
  249.     int n = nbytes;
  250.  
  251.     for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
  252.     out[0] = *in1;
  253.     out[1] = *in2;
  254.     out[2] = *in3;
  255.     out[3] = *in4;
  256.     }
  257.     return 0;
  258. }
  259.  
  260. /* Convert 4Mx12 to 4x12. */
  261. private int
  262. flip4x12(byte * buffer, const byte ** planes, int offset, int nbytes)
  263. {
  264.     byte *out = buffer;
  265.     const byte *pa = planes[0] + offset;
  266.     const byte *pb = planes[1] + offset;
  267.     const byte *pc = planes[2] + offset;
  268.     const byte *pd = planes[3] + offset;
  269.     int n = nbytes;
  270.  
  271.     /*
  272.      * We assume that the input is an integral number of pixels, and
  273.      * round up n to a multiple of 3.
  274.      */
  275.     for (; n > 0; out += 12, pa += 3, pb += 3, pc += 3, pd += 3, n -= 3) {
  276.     byte_var a1 = pa[1], b1 = pb[1], c1 = pc[1], d1 = pd[1];
  277.  
  278.     {
  279.         byte_var v0;
  280.  
  281.         out[0] = pa[0];
  282.         v0 = pb[0];
  283.         out[1] = (a1 & 0xf0) | (v0 >> 4);
  284.         out[2] = (byte) ((v0 << 4) | (b1 >> 4));
  285.         out[3] = pc[0];
  286.         v0 = pd[0];
  287.         out[4] = (c1 & 0xf0) | (v0 >> 4);
  288.         out[5] = (byte) ((v0 << 4) | (d1 >> 4));
  289.     }
  290.     {
  291.         byte_var v2;
  292.  
  293.         v2 = pa[2];
  294.         out[6] = (byte) ((a1 << 4) | (v2 >> 4));
  295.         out[7] = (byte) ((v2 << 4) | (b1 & 0xf));
  296.         out[8] = pb[2];
  297.         v2 = pc[2];
  298.         out[9] = (byte) ((c1 << 4) | (v2 >> 4));
  299.         out[10] = (byte) ((v2 << 4) | (d1 & 0xf));
  300.         out[11] = pd[2];
  301.     }
  302.     }
  303.     return 0;
  304. }
  305.  
  306. /* Convert NMx{1,2,4,8} to Nx{1,2,4,8}. */
  307. private int
  308. flipNx1to8(byte * buffer, const byte ** planes, int offset, int nbytes,
  309.        int num_planes, int bits_per_sample)
  310. {
  311.     /* This is only needed for DeviceN colors, so it can be slow. */
  312.     uint mask = (1 << bits_per_sample) - 1;
  313.     int bi, pi;
  314.     sample_store_declare_setup(dptr, dbit, dbbyte, buffer, 0, bits_per_sample);
  315.  
  316.     for (bi = 0; bi < nbytes * 8; bi += bits_per_sample) {
  317.     for (pi = 0; pi < num_planes; ++pi) {
  318.         const byte *sptr = planes[pi] + offset + (bi >> 3);
  319.         uint value = (*sptr >> (8 - (bi & 7) - bits_per_sample)) & mask;
  320.  
  321.         sample_store_next8(value, dptr, dbit, bits_per_sample, dbbyte);
  322.     }
  323.     }
  324.     sample_store_flush(dptr, dbit, bits_per_sample, dbbyte);
  325.     return 0;
  326. }
  327.  
  328. /* Convert NMx12 to Nx12. */
  329. private int
  330. flipNx12(byte * buffer, const byte ** planes, int offset, int nbytes,
  331.      int num_planes, int ignore_bits_per_sample)
  332. {
  333.     /* This is only needed for DeviceN colors, so it can be slow. */
  334.     int bi, pi;
  335.     sample_store_declare_setup(dptr, dbit, dbbyte, buffer, 0, 12);
  336.  
  337.     for (bi = 0; bi < nbytes * 8; bi += 12) {
  338.     for (pi = 0; pi < num_planes; ++pi) {
  339.         const byte *sptr = planes[pi] + offset + (bi >> 3);
  340.         uint value =
  341.         (bi & 4 ? ((*sptr & 0xf) << 8) | sptr[1] :
  342.          (*sptr << 4) | (sptr[1] >> 4));
  343.  
  344.         sample_store_next_12(value, dptr, dbit, dbbyte);
  345.     }
  346.     }
  347.     sample_store_flush(dptr, dbit, 12, dbbyte);
  348.     return 0;
  349. }
  350.  
  351. /* Flip data given number of planes and bits per pixel. */
  352. typedef int (*image_flip_proc) (P4(byte *, const byte **, int, int));
  353. private int
  354. flip_fail(byte * buffer, const byte ** planes, int offset, int nbytes)
  355. {
  356.     return -1;
  357. }
  358. private const image_flip_proc image_flip3_procs[13] = {
  359.     flip_fail, flip3x1, flip3x2, flip_fail, flip3x4,
  360.     flip_fail, flip_fail, flip_fail, flip3x8,
  361.     flip_fail, flip_fail, flip_fail, flip3x12
  362. };
  363. private const image_flip_proc image_flip4_procs[13] = {
  364.     flip_fail, flip4x1, flip4x2, flip_fail, flip4x4,
  365.     flip_fail, flip_fail, flip_fail, flip4x8,
  366.     flip_fail, flip_fail, flip_fail, flip4x12
  367. };
  368. typedef int (*image_flipN_proc) (P6(byte *, const byte **, int, int, int, int));
  369. private int
  370. flipN_fail(byte * buffer, const byte ** planes, int offset, int nbytes,
  371.        int num_planes, int bits_per_sample)
  372. {
  373.     return -1;
  374. }
  375. private const image_flipN_proc image_flipN_procs[13] = {
  376.     flipN_fail, flipNx1to8, flipNx1to8, flipN_fail, flipNx1to8,
  377.     flipN_fail, flipN_fail, flipN_fail, flipNx1to8,
  378.     flipN_fail, flipN_fail, flipN_fail, flipNx12
  379. };
  380.  
  381. /* Here is the public interface to all of the above. */
  382. int
  383. image_flip_planes(byte * buffer, const byte ** planes, int offset, int nbytes,
  384.           int num_planes, int bits_per_sample)
  385. {
  386.     if (bits_per_sample < 1 || bits_per_sample > 12)
  387.     return -1;
  388.     switch (num_planes) {
  389.  
  390.     case 3:
  391.     return image_flip3_procs[bits_per_sample]
  392.         (buffer, planes, offset, nbytes);
  393.     case 4:
  394.     return image_flip4_procs[bits_per_sample]
  395.         (buffer, planes, offset, nbytes);
  396.     default:
  397.     if (num_planes < 0)
  398.         return -1;
  399.     return image_flipN_procs[bits_per_sample]
  400.         (buffer, planes, offset, nbytes, num_planes, bits_per_sample);
  401.     }
  402. }
  403.